﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class VIPBuyDAL
    {
        /// <summary>
        /// 大于0表示成功,返回ID为BUYID
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="listDetail"></param>
        /// <returns></returns>
        public static int Add(VIPBuyInfo obj,List<VIPBuyDetailInfo> listDetail)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {

                    DbCommand dbCommand = DB.GetStoredProcCommand("VIPBuy_Add");
                    DB.AddInParameter(dbCommand, "UserId", DbType.Int32, obj.UserId);
                    DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
                    DB.AddInParameter(dbCommand, "DocNum", DbType.String, obj.DocNum);
                    DB.AddInParameter(dbCommand, "BuyDate", DbType.DateTime, obj.BuyDate);
                    DB.AddInParameter(dbCommand, "CustomerID", DbType.Int32, obj.CustomerID);
                    DB.AddInParameter(dbCommand, "VIPCardNum", DbType.String, obj.VIPCardNum);
                    DB.AddInParameter(dbCommand, "VIPCardType", DbType.String, obj.VIPCardType);
                    DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
                    DB.AddInParameter(dbCommand, "Tel", DbType.String, obj.Tel);
                    DB.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
                    DB.AddInParameter(dbCommand, "TotalMoney", DbType.Decimal, obj.TotalMoney);
                    DB.AddInParameter(dbCommand, "DiscTotalMoney", DbType.Decimal, obj.DiscTotalMoney);
                    DB.AddInParameter(dbCommand, "TotalPoint", DbType.Int32, obj.TotalPoint);
                    DB.AddInParameter(dbCommand, "AgentTotalPoint", DbType.Int32, obj.AgentTotalPoint);
                    DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

                    //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                    int BuyId = Convert.ToInt32(DB.ExecuteScalar(dbCommand, trans));
                    foreach(VIPBuyDetailInfo itm in listDetail)
                    { 
                        DbCommand dbCommandDetail = DB.GetStoredProcCommand("VIPBuy_AddDetail");
                        DB.AddInParameter(dbCommandDetail, "BuyId", DbType.Int32, BuyId);
                        DB.AddInParameter(dbCommandDetail, "ItemCode", DbType.String, itm.ItemCode);
                        DB.AddInParameter(dbCommandDetail, "ItemName", DbType.String, itm.ItemName);
                        DB.AddInParameter(dbCommandDetail, "Price", DbType.Decimal, itm.Price);
                        DB.AddInParameter(dbCommandDetail, "Quantity", DbType.Decimal, itm.Quantity);
                        DB.AddInParameter(dbCommandDetail, "LineTotal", DbType.Decimal, itm.LineTotal);
                        DB.AddInParameter(dbCommandDetail, "DiscPrcnt", DbType.Decimal, itm.DiscPrcnt);
                        DB.AddInParameter(dbCommandDetail, "IsDiscPiece", DbType.Boolean, itm.IsDiscPiece);
                        DB.AddInParameter(dbCommandDetail, "Point", DbType.Int32, itm.Point);
                        DB.AddInParameter(dbCommandDetail, "AgentPoint", DbType.Int32, itm.AgentPoint);
                        DB.AddInParameter(dbCommandDetail, "Remark", DbType.String, itm.Remark);

                        //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                        DB.ExecuteScalar(dbCommandDetail,trans);
                    }

                    trans.Commit();
                    return BuyId;

                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/> 错误:{1}", "VIPBuyDAL.Add", exc.Message));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return 0;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }

            }
        }


        public static bool Delete(int BuyId)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {
                    DbCommand dbCommand = DB.GetStoredProcCommand("VIPBuy_Delete", BuyId);
                    DB.ExecuteScalar(dbCommand, trans);
                    trans.Commit();                   
                    return true;
                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/>.ID:{2}<BR/> 错误:{1}", "VIPCustomerExchangeDAL.Delete", exc.Message, BuyId));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return false;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }
        }
        public static IDataReader GetAllDetail(int BuyId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPBuy_GetAllDetail", BuyId);
            return DB.ExecuteReader(dbCommand);
        }
        public static IDataReader GetList(int BuyId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPBuy_GetList", BuyId);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPBuy_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        
        /// <summary>
        /// 返回1，表示成功0表示失败
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="listDetail"></param>
        /// <returns></returns>
        public static int UpdatePass(VIPBuyInfo obj,List<VIPBuyDetailInfo> listDetail)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {

                    DbCommand dbCommand = DB.GetStoredProcCommand("VIPBuy_UpdatePass");
                    DB.AddInParameter(dbCommand, "BuyId", DbType.Int32, obj.BuyId);
                    DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
                    DB.AddInParameter(dbCommand, "Tel", DbType.String, obj.Tel);
                    DB.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
                    DB.AddInParameter(dbCommand, "TotalMoney", DbType.Decimal, obj.TotalMoney);
                    DB.AddInParameter(dbCommand, "DiscTotalMoney", DbType.Decimal, obj.DiscTotalMoney);
                    DB.AddInParameter(dbCommand, "TotalPoint", DbType.Int32, obj.TotalPoint);
                    DB.AddInParameter(dbCommand, "AgentTotalPoint", DbType.Int32, obj.AgentTotalPoint);
                    DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);
                    DB.ExecuteScalar(dbCommand,trans);

                    DbCommand dbCommandDel = DB.GetStoredProcCommand("VIPBuy_DeleteDetail", obj.BuyId);
                    DB.ExecuteScalar(dbCommandDel,trans);

                    foreach (VIPBuyDetailInfo itm in listDetail)
                    {
                        DbCommand dbCommandDetail = DB.GetStoredProcCommand("VIPBuy_AddDetail");
                        DB.AddInParameter(dbCommandDetail, "BuyId", DbType.Int32, obj.BuyId);
                        DB.AddInParameter(dbCommandDetail, "ItemCode", DbType.String, itm.ItemCode);
                        DB.AddInParameter(dbCommandDetail, "ItemName", DbType.String, itm.ItemName);
                        DB.AddInParameter(dbCommandDetail, "Price", DbType.Decimal, itm.Price);
                        DB.AddInParameter(dbCommandDetail, "Quantity", DbType.Decimal, itm.Quantity);
                        DB.AddInParameter(dbCommandDetail, "LineTotal", DbType.Decimal, itm.LineTotal);
                        DB.AddInParameter(dbCommandDetail, "DiscPrcnt", DbType.Decimal, itm.DiscPrcnt);
                        DB.AddInParameter(dbCommandDetail, "IsDiscPiece", DbType.Boolean, itm.IsDiscPiece);
                        DB.AddInParameter(dbCommandDetail, "Point", DbType.Int32, itm.Point);
                        DB.AddInParameter(dbCommandDetail, "AgentPoint", DbType.Int32, itm.AgentPoint);
                        DB.AddInParameter(dbCommandDetail, "Remark", DbType.String, itm.Remark);

                        //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                        DB.ExecuteScalar(dbCommandDetail, trans);
                    }


                    trans.Commit();
                    return 1;
                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/> 错误:{1}", "VIPBuyDAL.UpdatePass", exc.Message));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return 0;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }
        }
    }
}
